package com.bfsuol.yihang.customers.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.yihang.customers.api.QueryCustomerService;
import com.bfsuol.yihang.customers.domain.CustomerService;
import com.bfsuol.yihang.customers.dto.CustomerDTO;
import com.bfsuol.yihang.customers.entity.CustomerEntity;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QueryCustomerServiceImpl implements QueryCustomerService {

    @Autowired
    private CustomerService customerService;

    @Override
    public Page<CustomerDTO> page(Page page1, CustomerDTO customerParam) {
/*        QueryWrapper<CustomerEntity> queryWrapper = new QueryWrapper();

        if(!StringUtils.isBlank(customerParam.getName())){
            queryWrapper.lambda().like(CustomerEntity::getName,customerParam.getName());
        }
        if(!StringUtils.isBlank(customerParam.getPhone())){
            queryWrapper.lambda().like(CustomerEntity::getPhone,customerParam.getPhone());
        }
        if(customerParam.getStatus()!=null){
            queryWrapper.lambda().eq(CustomerEntity::getStatus,customerParam.getStatus());
        }
        queryWrapper.lambda().eq(CustomerEntity::getIsDelete,0);*/

        CustomerEntity customerEntity = new CustomerEntity();
        BeanUtils.copyProperties(customerParam,customerEntity);
        customerEntity.setCurrent((page1.getCurrent()-1)*page1.getSize());
        customerEntity.setSize(page1.getSize());
        List<CustomerEntity> customerEntities = customerService.getBaseMapper().selectByLists(customerEntity);
        List<CustomerDTO> list = Lists.newArrayList();
        customerEntities.forEach(item -> {
            CustomerDTO customerDTO = new CustomerDTO();
            BeanUtils.copyProperties(item, customerDTO);
            list.add(customerDTO);
        });
        Long count = customerService.getBaseMapper().selectCountBylists(customerEntity);
        Page<CustomerDTO> pages1 = new Page<>();
        BeanUtils.copyProperties(page1,pages1);
        pages1.setRecords(list);
        pages1.setTotal(count);
        return pages1;
    }

    @Override
    public CustomerDTO getById(Long id) {
        CustomerDTO customerDTO = new CustomerDTO();
        CustomerEntity customerEntity = customerService.getById(id);
        BeanUtils.copyProperties(customerEntity, customerDTO);
        return customerDTO;
    }
}
